﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.Units;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using ProbeMod.Exhibits;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 光子过载
    /// </summary>
    public sealed class ProbePhotonOverloadDef : ProbeCardTemplate
    {


        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Attack;
            config.TargetType = TargetType.RandomEnemy;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = new ManaGroup() { Blue = 2, Any = 1 };

            config.Damage = 11;
            config.UpgradedDamage = 16;

            config.Keywords = Keyword.Accuracy;
            config.UpgradedKeywords = Keyword.Accuracy;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 光子过载
    /// </summary>
    [EntityLogic(typeof(ProbePhotonOverloadDef))]
    public sealed class ProbePhotonOverload : ProbeCard
    {
        public int AttackCount
        {
            get => Battle.HandZone.Where((Card hand) => hand.Config.Type == CardType.Friend).Count();
        }
        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            //  将抽牌堆中建筑牌移入手中
            if (base.Battle.DrawZone.Count > 0)
            {
                if (Battle.DrawZone.Count>0 && Battle.HandIsNotFull)
                {
                    List<Card> list = Enumerable.ToList(Enumerable.Where(base.Battle.DrawZone, (Card card2) => card2.CardType == CardType.Friend));
                    if (list.Count > 0)
                    {
                        Card card = ((IEnumerable<Card>)list).Sample(base.GameRun.BattleRng);
                        yield return new MoveCardAction(card, CardZone.Hand);
                    }
                }
            }

            for (int i = 0; i < AttackCount; i++)
            {
                if (base.Battle.BattleShouldEnd) 
                {
                    yield break;
                }

                EnemyUnit randomAliveEnemy = base.Battle.RandomAliveEnemy;
                if (randomAliveEnemy != null)
                {
                    NotifyActivating();
                    yield return new DamageAction(base.Battle.Player, randomAliveEnemy, Damage, "单发激光");
                }
            }
        }
    }
}
